<?php

	class node {
		var $id;
		var $name;
		var $type;
		var $children = array();

		function __construct( $obj ) {
			$this->id = $obj->id;
			$this->name = $obj->name;
			$this->type = "node";
		}

		function prepare() {
			global $objList;
			$nodeObjs = array();
			foreach( $objList as $obj ) {
				if ( $obj->parentID == $this->id ) {
					$nodeObjs[] = new node($obj);
				}
			}
			foreach( $nodeObjs as $nodeObj ) {
				$nodeObj->prepare();
				$this->children[] = $nodeObj;
			}
			if ( $this->id == "0" ) $this->id = "root";
		}
	}

	include( "bootstrap.php" );

	$projectid = 1;
	$result = query("SELECT * FROM ".T_NODES." WHERE projectID={$projectid} ORDER BY parentID ASC");
	$objList = array();
	while ( $obj = mysql_fetch_object($result) ) $objList[] = $obj;

	$obj = new stdClass();
	$obj->id = "0";
	$obj->name = "Project";
	$root = new node( $obj );
	$root->prepare();
	
	$json = array( "identifier" => "id", "label" => "name", "items" => array());
	$json["items"][] = $root;
	
	$json = json_encode($json);
	$json = json_decode($json);
	echo "<pre>";
	echo print_r($json,tr);
	echo "</pre>";
?>
